package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Praise;
import cn.wolfcode.trip.base.domain.PraiseNumber;
import cn.wolfcode.trip.base.mapper.PraiseMapper;
import cn.wolfcode.trip.base.mapper.PraiseNumberMapper;
import cn.wolfcode.trip.base.query.PraiseQueryObject;
import cn.wolfcode.trip.base.service.IPraiseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PraiseServiceImpl implements IPraiseService {

    @Autowired
    private PraiseMapper praiseMapper;
    @Autowired
    private PraiseNumberMapper praiseNumberMapper;
    @Override
    public Praise get(Long userId, Long typeId, Integer type) {
        Praise praise = praiseMapper.selectByPrimaryKey(userId, typeId, type);
        return praise;
    }

    @Override
    public void save(Long userId, Long typeId, Integer type) {
        Praise p = praiseMapper.selectByPrimaryKey(userId, typeId, type);
        PraiseNumber number = praiseNumberMapper.get(typeId, type);
        PraiseNumber praiseNumber = new PraiseNumber();
        if (p == null){//没有数据
            Praise praise = new Praise();
            praise.setReader(0);
            praise.setState(1);
            praise.setType(type);
            praise.setTypeId(typeId);
            praise.setUserId(userId);
            praise.setPraiseDate(new Date());
            praiseMapper.insert(praise);//新增点赞信息
            if (number == null){//如果文章或内容还没有点过赞
                praiseNumber.setTypeId(typeId);
                praiseNumber.setType(type);
                praiseNumber.setNumber(1);
                praiseNumberMapper.insert(praiseNumber);//新增点赞数
            }else{
                praiseNumber.setType(type);
                praiseNumber.setTypeId(typeId);
                praiseNumber.setNumber(number.getNumber() + 1);
                praiseNumberMapper.update(praiseNumber);
            }

        } else if (p.getState() == 0){
            p.setState(1);
            p.setPraiseDate(new Date());
            praiseMapper.updateByPrimaryKey(p);
            if (number == null){//如果文章或内容还没有点过赞
                praiseNumber.setTypeId(typeId);
                praiseNumber.setType(type);
                praiseNumber.setNumber(1);
                praiseNumberMapper.insert(praiseNumber);//新增点赞数
            }else{
                praiseNumber.setType(type);
                praiseNumber.setTypeId(typeId);
                praiseNumber.setNumber(number.getNumber() + 1);
                praiseNumberMapper.update(praiseNumber);
            }

        }else if (p.getState() == 1){
            p.setState(0);
            p.setPraiseDate(new Date());
            praiseMapper.updateByPrimaryKey(p);
            if (number == null){//如果文章或内容还没有点过赞
                praiseNumber.setTypeId(typeId);
                praiseNumber.setType(type);
                praiseNumber.setNumber(1);
                praiseNumberMapper.insert(praiseNumber);//新增点赞数
            }else{
                praiseNumber.setType(type);
                praiseNumber.setTypeId(typeId);
                praiseNumber.setNumber(number.getNumber() - 1);
                praiseNumberMapper.update(praiseNumber);
            }
        }
    }

    @Override
    public PageInfo queryPraiseByReceiverId(PraiseQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
       List<Praise> list =praiseMapper.selectByReceiverId(qo);
        return new PageInfo(list);
    }

    @Override
    public void setRead(Long id) {
        praiseMapper.setRead(id);
    }
}
